﻿//
// Created by HuiYi on 2022/12/16.
//
#pragma once
#ifndef XL_XEXPAND_DWEBX_LIB_REF_REFREGISTER_H
#define XL_XEXPAND_DWEBX_LIB_REF_REFREGISTER_H
#include "pch.h"





//typedef unsigned int UINT;

inline bool WoRefFillFuncInt(void* ptr, const char* row) {
    *((int*)ptr) = atoi(row);
    return true;
}
inline void WoRefSezFuncInt(void* ptr, CXTextA&rData) {
    rData=std::to_string(*((int*)ptr));
}
inline bool WoRefFillFuncUint(void* ptr, const char* row) {
    *((UINT*)ptr) = static_cast<unsigned int>(atoi(row));
    return true;
}
inline void WoRefSezFuncUint(void* ptr, CXTextA& rData) {
    rData = std::to_string(*((UINT*)ptr));
}
inline bool WoRefFillFuncFloat(void* ptr, const char* row) {
    *((float*)ptr) = atof(row);
    return true;
}
inline void WoRefSezFuncFloat(void* ptr, CXTextA& rData) {
    rData = std::to_string(*((float*)ptr));
}

inline bool WoRefFillFuncDouble(void* ptr, const char* row) {
    *((double*)ptr) = atof(row);
    return true;
}
inline void WoRefSezFuncDouble(void* ptr, CXTextA& rData) {
    rData = std::to_string(*((double*)ptr));
}

inline bool WoRefFillFuncLong(void* ptr, const char* row) {
    *((long*)ptr) = atol(row);
    return true;
}
inline void WoRefSezFuncLong(void* ptr, CXTextA& rData) {
    rData = std::to_string(*((long*)ptr));
}

inline bool WoRefFillFuncLongLong(void* ptr, const char* row) {
    *((long long*)ptr) = atoll(row);
    return true;
}
inline void WoRefSezFuncLongLong(void* ptr, CXTextA& rData) {
    rData = std::to_string(*((long long*)ptr));
}

inline bool WoRefFillFuncBOOL(void* ptr, const char* row) {
    bool bk = false;
    bool rbool = false;
    if (strcmp(row, "true") == 0) {
        rbool = true;
    }
    else if (strcmp(row, "True") == 0) {
        rbool = true;
    }
    else if (strcmp(row, "TRUE") == 0) {
        rbool = true;
    }
    else if (strcmp(row, "真") == 0) {
        rbool = true;
    }
    else if (strcmp(row, "false") == 0) {
        rbool = false;
    }
    else if (strcmp(row, "False") == 0) {
        rbool = false;
    }
    else if (strcmp(row, "FALSE") == 0) {
        rbool = false;
    }
    else if (strcmp(row, "假") == 0) {
        rbool = false;
    }
    *((bool*)ptr) = rbool;
    return true;
}
inline void WoRefSezFuncBOOL(void* ptr, CXTextA& rData) {
    rData = std::to_string(*((bool*)ptr));
}

inline bool WoRefFillFuncCXTextA(void* ptr, const char* row) {
    *((CXTextA*)ptr) = row;
    return true;
}
inline void WoRefSezFuncCXTextA(void* ptr, CXTextA& rData) {
    rData = *((CXTextA*)ptr);
}

inline bool WoRefFillFuncString(void* ptr, const char* row) {
    *((std::string*)ptr) = row;
    return true;
}
inline void WoRefSezFuncString(void* ptr, CXTextA& rData) {
    rData = *((std::string*)ptr);
}

inline void WoRefInit() {
    struct _refInitModel {
        std::string typeName;
        int size;
        WoRefBaseModelFunc refFunc;
        WoRefBaseModelSezFunc sezFunc;
    };
    std::vector<_refInitModel> regList = {
            {typeid(int).name(),sizeof(int),WoRefFillFuncInt,WoRefSezFuncInt},
            {typeid(UINT).name(),sizeof(UINT), WoRefFillFuncUint,WoRefSezFuncUint},
            {typeid(double).name(),sizeof(double), WoRefFillFuncDouble,WoRefSezFuncDouble},
            {typeid(float).name(),sizeof(float), WoRefFillFuncFloat,WoRefSezFuncFloat},
            {typeid(long).name(),sizeof(long), WoRefFillFuncLong,WoRefSezFuncLong},
            {typeid(long long).name(),sizeof(long long), WoRefFillFuncLongLong,WoRefSezFuncLongLong},
            {typeid(bool).name(),sizeof(bool), WoRefFillFuncBOOL,WoRefSezFuncBOOL  },
            {typeid(CXTextA).name(),sizeof(CXTextA), WoRefFillFuncCXTextA,WoRefSezFuncCXTextA},
            {typeid(std::string).name(),sizeof(std::string), WoRefFillFuncString,WoRefSezFuncString},
    };
    for (auto& regInfo : regList) {
        WoRefRegBaseModelEx(regInfo.typeName.c_str(), regInfo.size, regInfo.refFunc, regInfo.sezFunc);
    }

}

static int veclen1 = strlen("class std::vector<");
static int cxveclen1 = strlen("class CXVector<");

inline bool WoRefModelIfisList(const char* TypeName, CXTextA* ArrType, WoRefArrarType* atype) {
    std::string typeName = TypeName;
    if (typeName.size() < veclen1 || typeName.size() < cxveclen1) {
        if (atype != nullptr) {
            *atype = WoRefArrarType::null;
        }
        return false;
    }
    if (typeName.substr(0, veclen1) == "class std::vector<") {
        //TODO: std的stl解析暂时没做完
        if (atype != nullptr) {
            *atype = WoRefArrarType::cxvector;
        }
        return true;
    }
    else if (typeName.substr(0, cxveclen1) == "class CXVector<") {
        if (ArrType != nullptr) {
            (*ArrType) = typeName.substr(cxveclen1, typeName.size() - cxveclen1 - 1);
        }
        if (atype != nullptr) {
            *atype = WoRefArrarType::cxvector;
        }
        return true;
    }
    if (atype != nullptr) {
        *atype = WoRefArrarType::null;
    }
    return false;
}

inline bool WoRefIfisList(std::string_view typeName,std::string*ArrType=nullptr, WoRefArrarType* atype=nullptr) {
	//class std::vector<int, class std::allocator<int> >
    //class CXVector<int>
    if (typeName.length()< veclen1|| typeName.length() < cxveclen1) {
		if (atype != nullptr) {
			*atype = WoRefArrarType::null;
		}
        return false;
    }
    if (typeName.substr(0, veclen1)=="class std::vector<") {
        //TODO: std的stl解析暂时没做完
		if (atype != nullptr) {
			*atype = WoRefArrarType::cxvector;
		}
        return true;
    }
    else if (typeName.substr(0, cxveclen1) == "class CXVector<") {
        if (ArrType!=nullptr) {
            (*ArrType) = typeName.substr(cxveclen1, typeName.length() - cxveclen1 - 1);
        }
        if (atype!=nullptr) {
            *atype = WoRefArrarType::cxvector;
        }
        return true;
    }
	if (atype != nullptr) {
		*atype = WoRefArrarType::null;
	}
    return false;
}

#endif //REF_TEST_REFREGISTER_HPP

